﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace UFSSWebApp
{
    public partial class popfiles : System.Web.UI.Page
    {
        UFSSservicereference.UserCreds uc = new UFSSservicereference.UserCreds();
        UFSSservicereference.UFSSserviceSoapClient ussc = new UFSSservicereference.UFSSserviceSoapClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            uc = (UFSSservicereference.UserCreds)Session["usercreds"];
            try
            {
                refresh();
                lblError.Visible = false;
            }
            catch
            {
                lblError.Visible = true;
                lblError.Text = "There was a problem completing your request... please try again later";
            }
            refresh();
        }
        private void refresh()
        {

            UFSSservicereference.UFSSFile[] files = ussc.getTopFiles(uc, 10);

            //create dataset
            DataSet ds = new DataSet();
            DataTable dt = new DataTable("files");
            DataRow dr;

            //create columns
            DataColumn namecolumn, sizecolumn, datecolumn ,ownercolumn, downloadscolumn;
            datecolumn = new DataColumn("Date added");
            namecolumn = new DataColumn("Filename");
            sizecolumn = new DataColumn("Size");
            ownercolumn = new DataColumn("Owner");
            downloadscolumn = new DataColumn("Times downloaded");
            



            //insert columns
            dt.Columns.Add(datecolumn);
            dt.Columns.Add(namecolumn);
            dt.Columns.Add(sizecolumn);
            dt.Columns.Add(ownercolumn);
            dt.Columns.Add(downloadscolumn);
            


            //create headers

            //fill table
            foreach (UFSSservicereference.UFSSFile file in files)
            {
                dr = dt.NewRow();
                dr["Filename"] = file.filename.ToString();
                dr["Size"] = file.filesize.ToString() + " bytes";
                dr["Owner"] = ussc.getUserName(file.ownerid);
                dr["Date added"] = file.dateadded.ToString();
                dr["Times downloaded"] = file.downloads.ToString();
               

                dt.Rows.Add(dr);
            }
            ds.Tables.Add(dt);

            //populate gridview
            DataView dv = new DataView(ds.Tables["files"]);
            dv = ds.Tables[0].DefaultView;

            
            gvPopFiles.DataSource = ds.Tables["files"];
            gvPopFiles.AutoGenerateColumns = true;
            gvPopFiles.DataBind();
            for (int i = 0; i < files.Length; i++)
            {
                gvPopFiles.Rows[i].Cells[1].Text = "<a href=\"userfile.aspx?id=" + files[i].userid + "\">" + files[i].filename + "</a>";
                gvPopFiles.Rows[i].Cells[3].Text = "<a href=\"userpage.aspx?id=" + gvPopFiles.Rows[i].Cells[3].Text + "\">" + gvPopFiles.Rows[i].Cells[3].Text + "</a>";
            }
        }
    }
}